<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>数据库交互</title>
    <!-- Bootstrap -->
    <link href="bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">

  </head>
  <body>
  <h1>数据列表</h1>
  <form action= "c.php" method = "get">
  <button class="btn btn-lg btn-default" type="submit">添加</button>


<?php
header("content-type:text/html;charset=utf-8");

$hostname = "127.0.0.1";
$username = "root";
$password = "root";
$database = "pro";

$connection = mysql_connect($hostname,$username,$password) or exit(mysql_error());
mysql_query("set names 'utf8'");//设置字符集
mysql_select_db($database,$connection) or exit(mysql_error());//选择数据库



$pagesize = 2;
$rs = mysql_query("select count(*) from product",$connection);
$myrow = mysql_fetch_array($rs);
$numrows = $myrow[0];



$pages = intval($numrows/$pagesize);
if($numrows%$pagesize) 
$pages++;
$page = isset($_GET['page'])?$_GET['page']:1;

  // if(isset($_GET['page'])){
  //   $page = intval($_GET['page']);
  // }else{
  //   $page = 1;
  // }另一种写法

$offset = $pagesize *($page-1);
$rs=mysql_query("select * from product order by id asc limit $offset,$pagesize",$connection);

?>

  <table class="table table-bordered">
    <thead>
      <tr>
        <th>ID</th>
        <th>名称</th>
        <th>标题</th>
        <th>类型</th>
        <th>价格</th>
        <th>操作</th>
      </tr>
    </thead>
<tbody>
<?php
$list = array();
while($tmp=mysql_fetch_array($rs,MYSQL_ASSOC)){
$list[] = $tmp;
}
?>

<?php 
foreach($list as $myrowvalue){?>
<tr>
    <td><?php echo $myrowvalue['id'];?></td>
    <td><?php echo $myrowvalue['named'];?></td>
    <td><?php echo $myrowvalue['title'];?></td>
    <td><?php echo $myrowvalue['type'];?></td>
    <td><?php echo $myrowvalue['price'];?></td>
    <td><a href="a.php?id=<?php echo $myrowvalue['id'];?>"><span class="label label-info">修改</span></a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="b.php?id=<?php echo $myrowvalue['id'];?>"><span class="label label-danger">删除</span></a></td>
</tr>
</tbody>
<?php } ?>
</table>

<?php 
echo "<div align='center'>共有"."{$pages}"."页(".$page."/".$pages.")";
      $first=1;
      $prev=$page-1;
      $next=$page+1;
      $last=$pages;

      if ($page > 1){
      echo '<a href="fenye.php?page='.$first.'">首页</a>';
        echo '<a href="fenye.php?page='.$prev.'">上一页</a>';
      }
      if ($page < $pages){
              echo '<a href="fenye.php?page='.$next.'">下一页</a>';
              echo '<a href="fenye.php?page='.$last.'">尾页</a>';
      }

mysql_close($connection);

?>